package com.core.shared;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;

import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.hibernate.internal.util.xml.Origin;
import org.hibernate.internal.util.xml.XmlDocument;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/5/4
 * Time: 11:02
 * To change this template use File | Settings | File Templates.
 */
public class dom4j implements XmlDocument {

    public void createXml(String fileName) {
        Document document = DocumentHelper.createDocument();
        Element employees = document.addElement("employees");
        Element employee = employees.addElement("employee");
        Element name = employee.addElement("name");
        name.setText("ddvip");
        Element sex = employee.addElement("sex");
        sex.setText("m");
        Element age = employee.addElement("age");
        age.setText("29");
        try {
            Writer fileWriter = new FileWriter(fileName);
            XMLWriter xmlWriter = new XMLWriter(fileWriter);
            xmlWriter.write(document);
            xmlWriter.close();
        } catch (IOException e) {

            System.out.println(e.getMessage());
        }


    }


    public static void parserXml(String fileName) {
//        File inputXml = new File(fileName);
//        SAXReader saxReader = new SAXReader();
//
//            Document document = saxReader.read(inputXml);
//            Element employees = document.getRootElement();
//            for (Iterator i = employees.elementIterator(); i.hasNext(); ) {
//                Element employee = (Element) i.next();
//                for (Iterator j = employee.elementIterator(); j.hasNext(); ) {
//                    Element node = (Element) j.next();
//                    System.out.println(node.getName() + ":" + node.getText());
//                }
//
//            }
//
        try {
            SAXReader sax = new SAXReader();//创建一个SAXReader对象
            File xmlFile = new File(fileName);//根据指定的路径创建file对象
            Document document = sax.read(xmlFile);//获取document对象,如果文档无节点，则会抛出Exception提前结束
            Element root = document.getRootElement();//获取根节点
            StringBuilder sb = new StringBuilder();
            getNodes(root, sb);//从根节点开始遍历所有节点
//            sb.append("}");
            System.out.println(sb);
//            listNodes(root);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void getNodes(Element node, StringBuilder sb) {
//        System.out.println("--------------------");
//        StringBuilder sb = new StringBuilder("");
//        sb.append("{");
        sb.append(node.getName()).append(":").append(node.getTextTrim());

//        System.out.println(node.getName() + ":" + node.getTextTrim());
//        List<Attribute> listAttr = node.attributes();//当前节点的所有属性的list
//        for (Attribute attr : listAttr) {//遍历当前节点的所有属性
//            String name = attr.getName();//属性名称
//            String value = attr.getValue();//属性的值
//            System.out.println(name + ":" + value);
//        }

        List<Element> listElement = node.elements();//所有一级子节点的list
        if (!listElement.isEmpty()) {
            sb.append("{");
            for (Element e : listElement) {//遍历所有一级子节点
                getNodes(e, sb);//递归
            }
            sb.append("},");
        } else {
            sb.append(",");
        }
    }

    public static void main(String[] args) {
        parserXml("D:\\z.xml");
    }

    @Override
    public Document getDocumentTree() {
        return null;
    }

    @Override
    public Origin getOrigin() {
        return null;
    }

    public static void listNodes(Element node) {
        StringBuilder sb = new StringBuilder();
        // 获取当前节点的所有属性节点
        List<Attribute> list = node.attributes();
        // 遍历属性节点
        for (Attribute attr : list) {
            System.out.println(attr.getText() + "-----" + attr.getName()
                    + "---" + attr.getValue());
        }

        if (!(node.getTextTrim().equals(""))) {
            System.out.println("文本内容：：：：" + node.getText());
        }

        // 当前节点下面子节点迭代器
        Iterator<Element> it = node.elementIterator();
        // 遍历
        while (it.hasNext()) {
            // 获取某个子节点对象
            Element e = it.next();
            // 对子节点进行遍历
            listNodes(e);
        }
    }


}